<template>
  <div class="login-account">
    <el-form :model="accountForm" :rules="accountRules" ref="formRef">
      <el-form-item label="账号" prop="userName">
        <el-input v-model="accountForm.userName" />
      </el-form-item>
      <el-form-item label="密码" prop="passWord">
        <el-input show-password v-model="accountForm.passWord" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, defineExpose, computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, type FormInstance } from 'element-plus'
import 'element-plus/theme-chalk/el-message.css'
import { accountRules } from '../config'
import { localCache } from '@/cache'
import { mapMenuToRoutes } from '@/utils/map-menus'
import { useStore } from '@/store'
const router = useRouter()
const store = useStore()
// 登录提交数据
const accountForm = reactive({
  userName: localCache.getItem('userName') ?? '',
  passWord: localCache.getItem('passWord') ?? ''
})
// 绑定form
const formRef = ref<FormInstance>()
// 提交表单
const loginAction = (isRemPwd: boolean) => {
  formRef.value?.validate((valid) => {
    if (valid) {
      if (isRemPwd) {
        localCache.setItem('userName', accountForm.userName)
        localCache.setItem('passWord', accountForm.passWord)
      } else {
        localCache.removeItem('userName')
        localCache.removeItem('passWord')
      }
      // 验证通过后的操作(根据自身业务进行编写后续逻辑)
      store.dispatch('login/loginAction', { ...accountForm })
    } else {
      ElMessage.error('校验未通过')
    }
  })
}

// 暴露出去的属性和方法
defineExpose({
  loginAction
})
</script>

<style lang="less" scoped>
.login-account {
}
</style>
